from collections import deque
import random
from DefaultCard import *
from DefaultSet import *
from BaseSet import *

class Player():
	def __init__(self,name="Player"):
                self.Name = name
		self.Deck = deque()
		self.Discard = deque()
		self.Played = deque()
		self.Gained = deque()
		self.Hand = []
		self.Coins = 0
		self.Actions = 1

		for i in range(7):
                        self.Deck.append(Copper())
                for i in range(3):
                        self.Deck.append(Estate())
                random.shuffle(self.Deck)
                self.drawHand()

        def drawHand(self):
                for i in range(5):
                        self.drawCard()
                self.printDebug()

        def drawCard(self):
                if len(self.Deck)>0:
                        self.Hand.append( self.Deck.pop() )
                else:
                        self.Deck.extend(self.Discard)
                        random.shuffle(self.Deck)
                        self.Discard.clear()
                        if len(self.Deck)>0:
                                self.Hand.append( self.Deck.pop() )

        def discardHand(self):
                self.Discard.extend(self.Hand)
                self.Discard.extend(self.Gained)
                self.Discard.extend(self.Played)
                self.Hand = []
                self.Gained.clear()
                self.Played.clear()

        def printState(self):
                print "Player: "+self.Name + " /// Actions: "+str(self.Actions)+" | Coins: "+str(self.Coins)

        def endTurn(self):
                self.discardHand()
                self.drawHand()
                self.Actions = 1
                self.Coins = 0

        def endActions(self):
                self.printState()
                for card in self.Hand:
                        if TYPE.Treasure in card.Type:
                                self.Coins += card.Value
                self.printState()

        def __str__(self):
                return self.Name

        def printDebug(self):
                self.printState()
                string = ""
                for card in self.Deck:
                        string += str(card)+", "
                string += " #"
                for card in self.Hand:
                        string += str(card)+", "
                string += "# "
                for card in self.Discard:
                        string += str(card)+", "
                string += " "
                print string

        def buy(self,card):
                if card!=None:
                        self.Gained.append(card)
                        self.Coins -= card.Cost

        def getCoins(self):
                return self.Coins

        def getActions(self):
                return self.Actions

        def getName(self):
                return self.Name

        
        
